<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}

$acts = array(
	'insert_contact', 'change_team_icon', 'insert_mission', 'insert_task', 'get_logs'
);
$act = in_array($_G['gp_act'], $acts, true) ? $_G['gp_act'] : '';
$dataid = intval(abs($_G['gp_dataid']));
if ( $act == 'insert_contact' ){
	$type = $_G['gp_type'];
	$content = htmlspecialchars($_G['gp_content']);
	if( !$type || !$content ){
		jsonp('资料填写不完整', 'alert', 1);
	}
	DB::Insert('customer_contacts', array(
		'adminid' => $admincp->adminid,
		'customerid' => $customer['id'],
		'type' => $type,
		'content' => $content,
		'create_time' => SYS_TIME
	));
	update('customer', $customer['id'], array(
		'last_contact_time' => SYS_TIME
	));
	jsonp('操作成功');
}else if( $act == 'change_team_icon' ){
	if( !$team ){
		jsonp('team error');
	}
	$i = intval(str_replace('i', '', trim($_G['gp_i'])));
	$c = intval(str_replace('c', '', trim($_G['gp_c'])));

	$update = array();
	if( $i ){
		$update['ficon'] = $i;
	}
	if( $c ){
		$update['fcolor'] = $c;
	}
	if( $update ){
		update('team', $team['id'], $update);
	}
}else if( $act == 'insert_mission' ){
	if( !$team ){
		jsonp('team error');
	}
	if( $_G['gp_title'] ){
		$dataid = DB::Insert('team_mission', array(
			'teamid' => $team['id'],
			'creatorid' => $admincp->adminid,
			'name' => $_G['gp_title'],
			'create_time' => SYS_TIME
		));
		DB::Insert('team_log', array(
			'teamid' => $team['id'],
			'adminid' => $admincp->adminid,
			'dataid' => $dataid,
			'type' => 'mission',
			'title' => '创建了任务组 ' . $_G['gp_title'],
			'create_time' => SYS_TIME
		));
		jsonp('success', 'ok', 0);
	}
}else if( $act == 'insert_task' ){
	if( !$team ){
		jsonp('team error');
	}
	$missionid = $_G['gp_missionid'];
	$adminid = abs($_G['gp_adminid']);
	$end_time = $_G['gp_end_time'] ? strtotime($_G['gp_end_time']) : 0;
	if( !$mission = fetch('team_mission', $missionid) ){
		jsonp('mission error');
	}

	if( $adminid && !$admincp->admins[$adminid] ){
		jsonp('admin error');
	}
	if( $_G['gp_title'] ){
		$dataid = DB::Insert('team_task', array(
			'teamid' => $team['id'],
			'creatorid' => $admincp->adminid,
			'adminid' => $adminid,
			'missionid' => $missionid,
			'name' => $_G['gp_title'],
			'create_time' => SYS_TIME,
			'begin_time' => SYS_TIME,
			'end_time' => $end_time ? $end_time : 0,
		));
		DB::Insert('team_log', array(
			'teamid' => $team['id'],
			'adminid' => $admincp->adminid,
			'dataid' => $dataid,
			'type' => 'task',
			'title' => '创建了任务 ' . $_G['gp_title'],
			'create_time' => SYS_TIME
		));
		jsonp('success', 'ok', 0);
	}
}else if( $act == 'get_logs' ){
	if( !$team ){
		jsonp('项目不存在');
	}

	$end_time = $_G['gp_end_time'] ? strtotime($_G['gp_end_time']) : strtotime(date('Y-m-d'));
	$end_time += 86399;
	$begin_time = $end_time - 86399;
	
	$next_time = $end_time + 86399;

	$where = array(
		'teamid' => $team['id'],
		"`create_time` > {$begin_time} AND `create_time` < {$end_time}"
	);
	$logs = limitquery('team_log', $where);
	$adminids = idserialize($logs, 'adminid');

	$admins = limitquery('admincp_member', array("`id` IN('{$adminids}')"));
	$members = $logDATASs = array();
	foreach( $admins as $k => $v ){
		$members[] = array(
			'id' => $v['id'],
			'heivrid' => $v['username'],
			'name' => heivrid($v),
			'avatar' => avatar($v['avatar']),
		);
	}
	foreach( $logs as $k => $v ){
		$v['heivrid'] = $admincp->admins[$v['adminid']]['username'];
		$v['name'] = heivrid($admincp->admins[$v['adminid']]);
		$v['avatar'] = avatar($admincp->admins[$v['adminid']]['avatar']);
		$logDATASs[$v['type']][$v['action']][] = $v;
	}
	$datas = array(
		'logs' => $logDATASs,
		'members' => $members,
		'end_time' => $end_time,
		'end_date' => date('m月d日', $end_time),
		'end_weekday' => str_replace(array(1, 2, 3, 4, 5, 6, 7), array('一', '二', '三', '四', '五', '六', '日'), date('N', $end_time)),

		'last_time' => date('Y-m-d', $begin_time - 1),
		'last_date' => date('m月d日', $begin_time - 1),
		'next_time' => date('Y-m-d', $next_time),
		'next_date' => date('m月d日', $next_time),
	);
	jsonp($datas, 'data', 0);
}